<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Usermodel extends Model {
	var $uid = null;
	var $login = null;
	var $pass = null;
	var $email = null;
	var $rez = null;
	
	var $groups = array();

	function __construct ()
	{
		parent::Model();
		$this->load->database();
	}
	function get ()
	{
		$this->db->where("uid", $this->uid);
		$query = $this->db->get('user', 1)->result();
		if (count($query)) {
			$this->uid = $query[0]->uid;
			$this->login = $query[0]->login;
			$this->pass = $query[0]->pass;
			$this->email = $query[0]->email;
			$this->rez = $query[0]->rez;
			
			// getting user groups
			$this->db->from('accessgroups');
			$this->db->join('access_group_user', 'accessgroups.id=access_group_user.group_id', 'INNER');
			$this->db->where("user_id", $this->uid);
			
			$this->groups = $this->db->get()->result();
			return $this;
		} else {
			return false;
		}
	}

	function login ()
	{
		$this->db->where("login", $this->login);
		$this->db->where("pass", $this->pass);
		$query = $this->db->get('user', 1)->result();
		if (count($query)) {
			$this->uid = $query[0]->uid;
			$this->login = $query[0]->login;
			$this->pass = $query[0]->pass;
			$this->email = $query[0]->email;
			$this->rez = $query[0]->rez;
			return true;
		} else {
			return false;
		}
	}

	function save () {
		$save = array(
			"login" => $this->login,
			"pass" => $this->pass,
			"email" => $this->email,
			"rez" => $this->rez
		);
		if ($this->uid) {
			$this->where("uid", $this->uid);
			return $this->db->update("user", $save);
		} else {
			return $this->db->insert("user", $save);
		}
	}
	function delete() {
		if ($this->uid) {
			$this->db->where("user", $this->uid);
			return $this->db->delete("user");
		}
	}
}

/* End of file user.php */
/* Location: ./system/application/models/user.php */